package com.mrobuy.jd.pic;

import com.gavel.annotation.FieldMeta;
import com.gavel.database.SQLExecutor;
import com.google.gson.Gson;
import com.jd.open.api.sdk.JdClient;
import com.jd.open.api.sdk.domain.supplier.ProductManagementService.response.get.*;
import com.jd.open.api.sdk.request.supplier.VcItemPrimaryPicGetRequest;
import com.jd.open.api.sdk.request.supplier.VcItemProductGetRequest;
import com.jd.open.api.sdk.response.supplier.VcItemPrimaryPicGetResponse;
import com.jd.open.api.sdk.response.supplier.VcItemProductGetResponse;
import com.mrobuy.jd.JdClientFactory;
import com.mrobuy.jdvc.entity.*;
import com.mrobuy.util.IdentifierGen;
import com.mrobuy.util.MD5Utils;

import java.util.ArrayList;
import java.util.List;

public class VcItemPrimaryPicGet {

    public static void main(String[] args) throws Exception {

        List<VCProductInfo> productInfos = SQLExecutor.executeQueryBeanList("select  * from VC_PRODUCT_INFO ", VCProductInfo.class);

        for (VCProductInfo productInfo : productInfos) {
            List<VCItemPic>  vcItemPics = load(productInfo, JdClientFactory.jdClient());
            SQLExecutor.update(productInfo);

            if ( vcItemPics!=null ) {
                for (VCItemPic vcItemPic : vcItemPics) {
                    vcItemPic.setId(IdentifierGen.uuid());
                    SQLExecutor.insert(vcItemPic);
                }
            }
        }


    }

    private static List<VCItemPic> load(VCProductInfo productInfo, JdClient client) throws Exception {

        VcItemPrimaryPicGetRequest request=new VcItemPrimaryPicGetRequest();
        request.setWareId(productInfo.getWareId());
        VcItemPrimaryPicGetResponse response = client.execute(request);
        System.out.println(new Gson().toJson(response));

        System.out.println("[" + response.getJosResultDto().getReturnCode() + "]" + response.getJosResultDto().getReturnMessage());


        List<VCItemPic> vcItemPics = new ArrayList<>();

        if ( "400".equalsIgnoreCase(response.getJosResultDto().getReturnCode()) ) {
            ItemPicApplyDto picApplyDto = response.getJosResultDto().getSingleObj();

            productInfo.setBrandId(picApplyDto.getBrandId());
            productInfo.setBrandName(picApplyDto.getBrandName());
            productInfo.setCategoryId(picApplyDto.getCategoryId());
            productInfo.setCategoryName(picApplyDto.getCategoryName());
            productInfo.setVendorCode(picApplyDto.getVendorCode());


            for (ItemPicSkuDto itemPicSkuDto : picApplyDto.getSkuList()) {


                if ( itemPicSkuDto.getImagePathDtoList()!=null ) {
                    for (ImagePathDto imagePathDto : itemPicSkuDto.getImagePathDtoList()) {
                        VCItemPic vcItemPic = new VCItemPic();

                        vcItemPic.setWareId(picApplyDto.getWareId());
                        vcItemPic.setSkuId(itemPicSkuDto.getSkuId());
                        vcItemPic.setIsprimary(imagePathDto.getIsPrimary());
                        vcItemPic.setPos(imagePathDto.getOrderSort());
                        vcItemPic.setType(PicType.PIC.getType()); // 方图
                        vcItemPic.setUrl(imagePathDto.getPath());

                        vcItemPics.add(vcItemPic);
                    }
                }
            }

            for (ItemPicSkuDtoLong itemPicSkuDto : picApplyDto.getSkuListLong()) {
                if ( itemPicSkuDto.getImagePathDtoListLong()!=null ) {
                    for (ImagePathDtoLong imagePathDto : itemPicSkuDto.getImagePathDtoListLong()) {
                        VCItemPic vcItemPic = new VCItemPic();
                        vcItemPic.setWareId(picApplyDto.getWareId());
                        vcItemPic.setSkuId(itemPicSkuDto.getSkuIdLong());
                        vcItemPic.setIsprimary(imagePathDto.getIsPrimaryLong());
                        vcItemPic.setPos(imagePathDto.getOrderSortLong());
                        vcItemPic.setType(PicType.LONG_PIC.getType()); // 长图
                        vcItemPic.setUrl(imagePathDto.getPathLong());
                        vcItemPics.add(vcItemPic);
                    }
                }
            }

            for (ItemPicSkuDtoLucency itemPicSkuDto : picApplyDto.getSkuListLucency()) {
                if ( itemPicSkuDto.getImagePathDtoListLucency()!=null ) {
                    for (ImagePathDtoLucency imagePathDto : itemPicSkuDto.getImagePathDtoListLucency()) {
                        VCItemPic vcItemPic = new VCItemPic();
                        vcItemPic.setWareId(picApplyDto.getWareId());
                        vcItemPic.setSkuId(itemPicSkuDto.getSkuIdLucency());
                        vcItemPic.setIsprimary(imagePathDto.getIsPrimaryLucency());
                        vcItemPic.setPos(imagePathDto.getOrderSortLucency());
                        vcItemPic.setType(PicType.LUCENCY_PIC.getType()); // 透明图
                        vcItemPic.setUrl(imagePathDto.getPathLucency());
                        vcItemPics.add(vcItemPic);
                    }
                }
            }

        }


        return vcItemPics;
    }


}
